﻿using HamcoWcs.Models.ForeEndModels.UITasks;
using HamcoWcs.Models.TibcoMessage.Items;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text;

namespace HamcoWcs.Models.MainTasks
{
    public class ExtendDto
    {

        /// <summary>
        /// MES/WCS下发任务时间戳
        /// </summary>
        public string TransActionId { get; set; }
        /// <summary>
        /// 中转储位
        /// </summary>
        public string HandOffShelf { get; set; } = "";
        /// <summary>
        /// 单机1|等待中转2|等待取货3
        /// </summary>
        public int SrmTaskStatus { get; set; } = 0;

        /// <summary>
        /// 当前执行的Srm
        /// </summary>
        public string ExcuteSrm { get; set; } = "";
        /// <summary>
        /// 备用Node
        /// </summary>
        public string AlternateNode { get; set; } = "";
        /// <summary>
        /// 原目的地
        /// </summary>
        public string OldDestination { get; set; } = "";
        /// <summary>
        /// 是否上报MES开始
        /// </summary>
        public bool IsReportStart { get; set; } = false;

        /// <summary>
        /// 起始地地址
        /// </summary>
        public MachineAddressDto FromAddress { get; set; }
        /// <summary>
        /// 目的地地址
        /// </summary>
        public MachineAddressDto ToAddress { get; set; }
        /// <summary>
        /// 盘点任务信息
        /// </summary>
        public InventoryTaskInfo? InventoryTaskInfo { get; set; }
        /// <summary>
        /// 物料信息
        /// </summary>
        public MaterialInfo? MaterialInfo { get; set; }

        static JsonSerializerSettings JSON_SERIALIZER_SETTINGS = new JsonSerializerSettings()
        {

        };

        public ExtendDto()
        {

        }



        public string ToJson()
        {
            return JsonConvert.SerializeObject(this);
        }

        public static ExtendDto Deserlize(string json)
        {
            return JsonConvert.DeserializeObject<ExtendDto>(json);
        }



    }

    /// <summary>
    /// MES提供的物料信息
    /// </summary>
    public class MaterialInfo
    {
        public MaterialInfo()
        {
        }

        /// <summary>
        /// 栈板名
        /// </summary>
        public string PalletName { get; set; }
        /// <summary>
        /// 物料数量（BOX|PALLET）
        /// </summary>
        public int MaterialCount => MaterialList?.Count ?? 0;
        /// <summary>
        /// 展板集合
        /// </summary>
        public List<string> MaterialList { get; set; }
        /// <summary>
        /// 箱子或栈板(Box | Pallet)
        /// </summary>
        public string PackingType { get; set; }
        /// <summary>
        /// 栈板尺寸代码
        /// </summary>
        public string PalletSettingCode { get; set; }
        /// <summary>
        /// 箱子尺寸代码
        /// </summary>
        public string MaterialSettingCode { get; set; }

        /// <summary>
        /// 输出为Plc需要的信息
        /// </summary>
        /// <returns></returns>
        public string? ToPlcInfo()
        {
            if (MaterialCount == 0) return "error";

            var sb = new StringBuilder();
            sb.Append(string.Join('|', MaterialList));
            return sb.ToString();
        }
    }
}
